package com.zhuidian.mothernote.presenter;

import android.content.Intent;

import com.base.request.JsonConvert;
import com.base.util.MyUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.ObservableBody;
import com.zhuidian.mothernote.activity.HomeActivity;
import com.zhuidian.mothernote.activity.WelcomeActivity;
import com.zhuidian.mothernote.bean.WelcomeBean;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

public class WelcomePresenter extends BasePresenterImp<WelcomeActivity> {

    @Override
    public void onCreate() {
    }

    public void getImageUrl(String url) {

        Observable<WelcomeBean> observable = (Observable<WelcomeBean>) OkGo.post(url)
                .params("", 1)
                .params("", 1)
                .converter(new JsonConvert<WelcomeBean>() {
                })
                .adapt(new ObservableBody<>());
        observable.observeOn(Schedulers.io());
        observable.doOnSubscribe(new Action0() {
            @Override
            public void call() {
                mView.showProgress();
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WelcomeBean>() {
            @Override
            public void onCompleted() {
                mView.hideProgress();
                jumpToHomePage();
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                mView.hideProgress();
                mView.showMsg(MyUtils.analyzeNetworkError(e));
                mView.onError();
            }

            @Override
            public void onNext(WelcomeBean welcomeBean) {
                mView.setImageUrl(welcomeBean);
            }
        });
    }

    public void jumpToHomePage() {
        Observable.timer(1, TimeUnit.SECONDS)
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onCompleted() {
                        mView.startActivity(new Intent(mView, HomeActivity.class));
                        mView.finish();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Long aLong) {

                    }
                });
    }
}
